<?php
global $_W, $_GPC;
$op = !empty($_GPC['op'])? $_GPC['op'] : 'base';
//权限控制
$tid=8157;
//
wl_load()->model('functions');
$checkfunction=checkfunc($tid);
$_W['page']['title'] = $checkfunction['name'];
if($op=='base')
{
	include wl_template('store/selflogistics');
}
//截止
		if($op=='display'){
			$list = pdo_fetchall("SELECT * FROM " . tablename('tg_dispatch') . " WHERE uniacid = '{$_W['uniacid']}' and dispatchtype=2 ORDER BY id asc");
			include wl_template('store/selflogistics');
		}elseif($op=='post'){
		
			$id = $_GPC['id'];
			$dispatch = pdo_fetch("SELECT * FROM " . tablename('tg_dispatch') . " WHERE uniacid = '{$_W['uniacid']}' and id = '{$id}'");
			if (!empty($dispatch)) {
				$dispatch_areas = unserialize($dispatch['areas']);
				$dispatch_carriers= unserialize($dispatch['carriers']);
				$dispatch_delivery= unserialize($dispatch['Deliverys']);
			} 
			$areafile = IA_ROOT . "/addons/feng_fightgroups/static/area/areas";
			$areas = json_decode(@file_get_contents($areafile), true);
			if (!is_array($areas)) {
				require_once IA_ROOT . "/addons/feng_fightgroups/static/area/xml2json.php";
				$file = IA_ROOT . "/addons/feng_fightgroups/static/area/Area.xml";
				$content = file_get_contents($file);
				$json = xml2json :: transformXmlStringToJson($content);
				$areas = json_decode($json, true);
				file_put_contents($areafile, $json);
			} 
			if (checksubmit('submit')) {
				$areas = array();
				$carrier = array();
				$delivery= array();
				$randoms = $_GPC['random'];
				$carriers=$_GPC['carriers'];
				$deliverys=$_GPC['deliverys'];
				$dispatch2 = pdo_fetch("SELECT * FROM " . tablename('tg_dispatch') . " WHERE uniacid = '{$_W['uniacid']}'");
				if (is_array($randoms)) {
					foreach($randoms as $random) {
						$areas[] = array('citys' => $_GPC['citys'][$random], 'firstprice' => $_GPC['firstprice'][$random], 'firstweight' => $_GPC['firstweight'][$random], 'secondprice' => $_GPC['secondprice'][$random], 'secondweight' => $_GPC['secondweight'][$random]);
					} 
				} 
				if (is_array($carriers)) {
					foreach($carriers as $random) {
						$carrier[] = array('address' => $_GPC['address'][$random], 'realname' => $_GPC['realname'][$random], 'mobile' => $_GPC['mobile'][$random], 'content' => $_GPC['content'][$random]);
					} 
				} 
				if (is_array($deliverys)) {
					foreach($deliverys as $random) {
						$delivery[] = array('cart' => $_GPC['cart'][$random], 'cost' => $_GPC['cost'][$random]);
					} 
				} 
				//message(iserializer($carrier));
				$data = array('uniacid' => $_W['uniacid'], 'displayorder' => intval($_GPC['displayorder']), 'dispatchtype' => 2, 'dispatchname' => trim($_GPC['dispatchname']), 'express' => trim($_GPC['express']), 'firstprice' => trim($_GPC['default_firstprice']), 'firstweight' => trim($_GPC['default_firstweight']), 'secondprice' => trim($_GPC['default_secondprice']), 'secondweight' => trim($_GPC['default_secondweight']), 'areas' => iserializer($areas), 'carriers' => iserializer($carrier), 'deliverys' => iserializer($delivery),'enabled' => intval($_GPC['enabled']));
				if (!empty($id)) {
					pdo_update('tg_dispatch', $data, array('id' => $id));
				} else {
					pdo_insert('tg_dispatch', $data);
				} 
				echo "<script>alert('更新成功！');location.href='".web_url('store/selflogistics')."';</script>";
				exit;
				
			} 
			include wl_template('store/selflogistics');
		}elseif($op=='delete'){
			$id = intval($_GPC['id']);
			$dispatch = pdo_fetch("SELECT id,dispatchname FROM " . tablename('tg_dispatch') . " WHERE id = '$id' AND uniacid=" . $_W['uniacid'] . "");
			if (empty($dispatch)) {
				message('抱歉，派送方式不存在或是已经被删除！', $this -> createWebUrl('area', array('op' => 'yunfei')), 'error');
			} 
			pdo_delete('tg_dispatch', array('id' => $id));
				echo "<script>alert('删除成功！');location.href='".web_url('store/selflogistics')."';</script>";
		exit;
			
		}else if ($op == 'tpl') {
	$limit = $_GPC['limit'];
	$random = random(16);
	ob_clean();
	ob_start();
	if($limit=='limit'){
		include $this -> template('arealimit');
	}else{
		include $this -> template('dispatch');
	}
	$contents = ob_get_contents();
	ob_clean();
	die(json_encode(array('random' => $random, 'html' => $contents)));
}
else if ($op == 'tpl1') {
	$limit = $_GPC['limit'];
	$carriers = random(16);
	ob_clean();
	ob_start();
	if($limit=='limit'){
		include $this -> template('arealimit');
	}else{
		include $this -> template('carrier');
	}
	$contents = ob_get_contents();
	ob_clean();
	die(json_encode(array('carriers' => $carriers, 'html' => $contents)));
}
else if ($op == 'tpl2') {
	
	$deliverys = random(16);
	ob_clean();
	ob_start();
	
	include wl_template('store/Delivery');
	$contents = ob_get_contents();
	ob_clean();
	die(json_encode(array('deliverys' => $deliverys, 'html' => $contents)));
}


exit();


?>